<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=8" />
    <title>Gestalt Sample: Simple Particles - Python</title>

    <script src="../../js/jquery.js" type="text/javascript"></script>

    <script src="../../js/gestalt.js" type="text/javascript"></script>

</head>
<body >

<script language="python">

# imports
from System import * # for Uri
from System.Windows import * 
from System.Windows.Shapes import * # exposes Rectangle to scope since not added by default
from System.Windows.Media.Animation import * # exposes Storyboard
from System.Windows.Media import * # exposes CompositionTarget
#from System.Windows.Media.Imaging import * # for bitmap
from System.Net import * # for WebClient
from System.Windows.Resources import * # for WebClient
#from System.Xml import *
#from System.Xml.Linq import *
from System.Diagnostics import * # enables outputing to a debug window!
from System.Windows.Markup import * # for XamlReader


# localize
layoutRoot = me.LayoutRoot

# locals
_snowflake = None
randomNumber = Random()

#
# create class
#
class Snowflake(UserControl):
    def __init__(self, xaml, randomNumber):
        self.xaml = xaml
        self.randomNumber = randomNumber
        
        ### CREATE INSTANCE VARIABLE ###
        self.x = 0
        self.y = 0
        self.xSpeed = 0
        self.ySpeed = 0
        self.radius = 0
        self.scale = 0
        self.alpha = 0
        self.stageSize = Point()
        
        self.Content = XamlReader.Load(self.xaml)
        
        #
        # This method gets called many times a second and is responsible for moving your snowflake around
        #
        def MoveSnowFlake(sender, e):
            self.x = self.x + self.xSpeed
            self.y = self.y + self.ySpeed

            Canvas.SetTop(self, self.y)
            Canvas.SetLeft(self, Canvas.GetLeft(self) + self.radius * Math.Cos(self.x))

            # Reset the position to go back to the top when the bottom boundary is reached
            if (Canvas.GetTop(self) > self.stageSize.Y):
                Canvas.SetTop(self, - self.ActualHeight - 10)
                self.y = Canvas.GetTop(self)
        

        CompositionTarget.Rendering += MoveSnowFlake
                 
            
    #
    # All of the various properties that you want to set initially is done here
    #
    def SetInitialProperties(self, stageWidth, stageHeight):
        # Setting the various parameters that need tweaking
        self.xSpeed = self.randomNumber.NextDouble() / 20
        self.ySpeed = .01 + self.randomNumber.NextDouble() * 2
        self.radius = self.randomNumber.NextDouble()
        self.scale = .01 + self.randomNumber.NextDouble() * 2
        self.alpha = .1 + self.randomNumber.NextDouble()

        # Setting initial position
        Canvas.SetLeft(self, self.randomNumber.Next(stageWidth))
        Canvas.SetTop(self, self.randomNumber.Next(stageHeight))

        self.stageSize = Point(stageWidth, stageHeight)

        self.y = Canvas.GetTop(self)

        # Setting initial size and opacity
        self.Content.Width = 5 * self.scale
        self.Content.Height = 5 * self.scale
        self.Content.Opacity = self.alpha
            
#
# Define event handlers before they are referenced below
#
def SnowflakeLoaded(sender, e):
    global _snowflake
    _snowflake = e.Result
    Main()
    
#
# define methods
#
def LoadSnowflake():
    client = WebClient()
    client.DownloadStringCompleted += SnowflakeLoaded
    client.DownloadStringAsync(Uri("../samples/particles/snowflake.xaml", UriKind.RelativeOrAbsolute))
    
def Main():
    global _snowflake
    global randomNumber
    
    for i in range(0, 200):
        snowflake = Snowflake(_snowflake, randomNumber);
        # 600 and 300 is the width/height of the application
        snowflake.SetInitialProperties(600, 300);
        layoutRoot.Children.Add(snowflake);

#
# Begin Loading XAML dependencies
#
LoadSnowflake()

</script>

    <!-- This isn't a part of the sample, but a link back to the Gestalt site -->
    <div id="upsell" style="color:#fff; font-size: 13px; font-family:'Lucida Grande','Lucida Sans Unicode',Helvetica,Arial,Lucida,Verdana,sans-serif; margin-bottom: 30px;">
        <a href="http://www.visitmix.com/labs/gestalt/samples/" style="background-color: #000; color: #fff; display: block; text-decoration: none;" onmouseover="this.style.color='#000'; this.style.background='#ccc'" onmouseout="this.style.color='#fff'; this.style.background='#000'">
		    <span style="display:block; padding: 8px; text-align:center;">&laquo; Check out other samples on the Gestalt web site</span>
	    </a>
    </div>
    
<!-- The following commented line of code is equivalent to the inline XAML
     and we recommend doing it this way. Only caveat, if you link to a XAML
     file, you'll be able to view the page correctly only through a server 
     (e.g. apache or IIS) because of security sandbox reasons. -->
     
    <!--<xml src="page.xaml" id="page" class="xaml" width="600" height="300" style="display:none;"></xml>-->
    
    <xml id="page" class="xaml" width="600" height="300" style="display:none;">
    
        <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="600" Height="300">
          <Canvas x:Name="LayoutRoot">
            <Canvas.Background>
              <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF00202A" Offset="1"></GradientStop>
                <GradientStop Color="#FF000000" Offset="0"></GradientStop>
              </LinearGradientBrush>
            </Canvas.Background>
          </Canvas>
        </UserControl>
    
    </xml>
    
</body>
</html>
